﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using UPNP.IO;

namespace RouterControl.Whs.SingletonService
{

    #region structures

    public struct RootDeviceInfo
    {
        public string SchemaId;
        public string Description;
        public string FriendlyName;
        public string ManufacturerName;
        public string ManufacturerURL;
        public string ModelName;
        public string ModelNumber;
        public string ModelURL;
        public string PresentationURL;
        public string Type;
        public string UniqueDeviceName;
        public string UPC;

        public RootDeviceInfo(RootDevice r)
        {
            if (r == null)
                throw new ArgumentNullException("r");

            this.SchemaId = r.SchemaId;
            this.Description = r.Description;
            this.FriendlyName = r.FriendlyName;
            this.ManufacturerName = r.ManufacturerName;
            this.ManufacturerURL = Convert.ToString(r.ManufacturerURL);
            this.ModelName = r.ModelName;
            this.ModelNumber = r.ModelNumber;
            this.ModelURL = Convert.ToString(r.ModelURL);
            this.PresentationURL = Convert.ToString(r.PresentationURL);
            this.Type = r.Type;
            this.UniqueDeviceName = r.UniqueDeviceName;
            this.UPC = r.UPC;
        }
    }

    public struct ErrorReportingStatus
    {
        public bool IsSubmitting;
        public float Progress;

        public int Submitted;
        public int Unsubmitted;
    }

    [Flags]
    public enum ExtendedEnabled
    {
        Disabled = 0,
        Enabled = 1,
        EnabledWithNotifications = 3
    }

    [Flags]
    public enum ApprovalStatus
    {
        All = 3,
        Approved = 1,
        Unapproved = 2
    }

    public struct PortForwardingEx
    {
        public CommonInternetGateway.PortForwarding Forwarding;
        public bool Approved;
    }

    public struct SettingsAll
    {
        public string SelectedDeviceId;
        public bool TrafficStatsEnabled;
        public ExtendedEnabled PortForwardingsMonitoringEnabled;
        public bool ErrorReportsSubmissionEnabled;
        public bool RouterHijackingDetectionEnabled;
    }

    #endregion

    [ServiceContract]
    public interface ISingletonService2
    {

        #region Misc

        [OperationContract]
        void Idle();

        #endregion

        #region device search

        [OperationContract]
        List<RootDeviceInfo> GetFoundDevices();

        [OperationContract]
        bool HasFoundDevices();

        [OperationContract]
        CommonInternetGateway.SummaryData? GetSelectedDeviceConfigData();

        [OperationContract]
        bool IsSearchingForDevices();

        [OperationContract]
        void SearchForDevices(bool waitUntilComplete);

        [OperationContract]
        void EnsureFirstSearchForDevices(bool waitUntilComplete);

        [OperationContract]
        bool IsDeviceSelected();

        [OperationContract]
        bool IsSelectedDeviceFound();

        [OperationContract]
        bool IsSelectedDeviceLost();

        [OperationContract]
        RootDeviceInfo? GetSelectedDevice();

        [OperationContract]
        string GetSelectedDeviceId();

        [OperationContract]
        void SetSelectedDeviceId(string devId);

        #endregion

        #region traffic stats

        [OperationContract]
        bool IsTrafficStatsEnabled();

        [OperationContract]
        void SetTrafficStatsEnabled(bool enable);

        #endregion

        #region router hijacking detection

        [OperationContract]
        bool IsRouterHijackingDetectionEnabled();

        [OperationContract]
        void SetRouterHijackingDetectionEnabled(bool enable);

        #endregion

        #region port forwarding

        [OperationContract]
        ExtendedEnabled IsPortForwardingsMonitoringEnabled();

        [OperationContract]
        void SetPortForwardingsMonitoringEnabled(ExtendedEnabled enable);

        [OperationContract]
        CommonInternetGateway.PortForwarding[] GetPortForwardings(ApprovalStatus status);

        [OperationContract]
        PortForwardingEx[] GetPortForwardingsEx();

        [OperationContract]
        void AddOrUpdatePortForwarding(CommonInternetGateway.PortForwarding fwd);

        [OperationContract]
        void DeletePortForwarding(CommonInternetGateway.PortForwarding fwd);

        [OperationContract]
        void SetPortForwardingApproval(CommonInternetGateway.PortForwarding fwd, bool approve);

        [OperationContract]
        void ClearAllPortForwardingApprovals();

        #endregion

        #region update checks

        [OperationContract]
        DateTime GetLastUpdateCheck();

        [OperationContract]
        Web.ReleaseInfo? GetNewerVersion();

        [OperationContract]
        Web.ReleaseInfo? CheckNewerVersionNow();

        #endregion

        #region schema upload

        [OperationContract]
        bool RequiresSelectedDeviceSchemaUpload();

        [OperationContract]
        bool UploadSelectedDeviceSchema();

        [OperationContract]
        Stream GetSelectedDeviceSchema();

        #endregion

        #region error reporting

        [OperationContract]
        bool IsErrorReportsSubmissionEnabled();

        [OperationContract]
        void SetErrorReportsSubmissionEnabled(bool enable);

        [OperationContract]
        ErrorReportingStatus GetErrorReportingStatus();

        #endregion

        #region all settings

        [OperationContract]
        SettingsAll GetAllSettings();

        [OperationContract]
        void SetAllSettings(SettingsAll s);

        #endregion
    }
}
